package com.winenine999.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.winenine999.model.Page;
import com.winenine999.model.Wineproduct;
import com.winenine999.service.IWinebrandService;
import com.winenine999.service.IWineproductService;
import com.winenine999.service.IWineryService;
import com.winenine999.service.IWinetypeService;
@Controller
@RequestMapping(value="/wineproduct")
public class WineproductController {
	@Autowired
	private IWineproductService wineproductService;
	@Autowired
	private IWineryService wineryService;
	@Autowired
	private IWinebrandService winebrandService;
	@Autowired
	private IWinetypeService winetypeService;
	@RequestMapping(value="/in",method=RequestMethod.GET)
	public String index(){
		return "common/main";
	}
	/**
	 * 进入wineproduct.list页面
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(){
	
		return "wineproduct/list";	
	}
	/**
	 * datatables请求controller
	 * @param request
	 * @param searchByName 根据名称检索的值
	 * @param searchByType 根据类型检索的值
	 * @return
	 */
	@RequestMapping(value="/wineproduct_list",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> wineproductList(@RequestParam(value="order[0][column]")String a,HttpServletRequest request,String searchByName,String searchByType){
		Map<String,Object> map=new HashMap<String,Object>();
		System.out.println("a:"+a);
		Map<String,Object> params1=new HashMap();
		Integer start=0,length=2,column=1;  
        length=2;  
        String sSearch="";  
        String draw="1";
        String dir="asc";
	Map<String,String[]>params=request.getParameterMap(); 
        for(String attr:params.keySet()){  
            String[] val=params.get(attr);   
            if(attr.equals("start"))  
                start=Integer.parseInt(val[0]);  
            if(attr.equals("length"))  
                length=Integer.parseInt(val[0]);  
            if(attr.equals("search[value]"))  
            	sSearch=val[0];//自带搜索框值，本项目已关闭该功能
            if(attr.equals("order[0][column]"))  
                column=Integer.parseInt(val[0]);//排序的列的编号1开始	            	
            if(attr.equals("order[0][dir]"))  
                dir=val[0]; //排序方式，有asc和desc两种
            if(attr.equals("draw"))  
                draw=val[0];	        
        } 
        if(column==2){
    		params1.put("column", "proname");//添加要排序的列(如果是1，pid排序)
    	}
    	if(column==3){
    		params1.put("column", "type");//添加要排序的列
    	}
    	if(column==11){
    		params1.put("column", "price");//添加要排序的列
    	}
    	if(dir.equals("asc")){
    		params1.put("dir", "asc");//传入排序方式
    	}
    	if(dir.equals("desc")){
    		params1.put("dir", "desc");//传入排序方式
    	}
    	params1.put("proname", searchByName);//传入自定义的搜索框，按name搜索
		params1.put("type", searchByType);//传入自定义的搜索框，按id搜索
        Page page=new Page(start,length);
        page.setParams(params1);
        Page page1= wineproductService.findPageAllWineproductsInfo(page);
		
        map.put("aaData", page1.getList());//查到的结果集
		map.put("recordsFiltered", page1.getTotal());//总条数，用于计算页数
		map.put("recordsTotal",  page1.getTotal());//总条数，用于页面显示：‘全部记录数  条’
		return map;	
	}
	@RequestMapping(value="/wineproductAddPre",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> wineproductAddPre(){
		Map<String,Object> map=new HashMap<String,Object>();
		List<Map<String,Object>> Winerymaps=wineryService.findAllWinerys();
		List<Map<String,Object>> Winebrandmaps=winebrandService.findAllWinebrands();
		List<Map<String,Object>> Winetypemaps=winetypeService.findAllWinetypes();
		map.put("Winerymaps", Winerymaps);
		map.put("Winebrandmaps", Winebrandmaps);
		map.put("Winetypemaps", Winetypemaps);		
		return map;
	}
	/**
	 * 增加wineproduct
	 * @param wineproduct
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody Integer add(Wineproduct wineproduct){
		System.out.println("wineproduct:"+wineproduct);
		
		Integer result=wineproductService.insertSelective(wineproduct);
		
		return result;	
	}
	/**
	 * 根据id删除product
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody Integer delete(Integer id){
		System.out.println("id:"+id);
		
		Integer result=wineproductService.deleteByPrimaryKey(id);
		
		return result;	
	}
	/**
	 * 修改wineproduct前
	 * @return
	 */
	@RequestMapping(value="/wineproductUpdatePre",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> wineproductUpdatePre(Integer id){
		Map<String,Object> map=new HashMap<String,Object>();
		List<Map<String,Object>> Winerymaps=wineryService.findAllWinerys();
		List<Map<String,Object>> Winebrandmaps=winebrandService.findAllWinebrands();
		List<Map<String,Object>> Winetypemaps=winetypeService.findAllWinetypes();
		Wineproduct wineproduct=wineproductService.selectByPrimaryKey(id);
		map.put("Winerymaps", Winerymaps);
		map.put("Winebrandmaps", Winebrandmaps);
		map.put("Winetypemaps", Winetypemaps);		
		map.put("wineproduct", wineproduct);		
		return map;
	}
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody Integer update(Wineproduct wineproduct){
		System.out.println("wineproduct:"+wineproduct);
		
		Integer result=wineproductService.updateByPrimaryKeySelective(wineproduct);
		
		return result;	
	}
	
}
